﻿using System;
using System.Net;
using Stimulus360.Repositories;
using Microsoft.ServiceModel.Web;
using System.Collections.Generic;
using Stimulus360.Services.Interfaces;
using Stimulus360.Models.Data.CustomTypes;
using Stimulus360.Repositories.Interfaces;

namespace Stimulus360.Services
{
    /// <summary>
    ///     This class is used to interact with the IDataRepository to perform
    ///     to perform logical and CRUD operations on the InvestmentCategory entity.
    /// </summary>
    public class InvestmentCategoryService : IInvestmentCategoryService
    {
        #region IInvestmentCategoryService Members

        /// <summary>
        ///     An instance of the IDataRepository
        /// </summary>
        private readonly IDataRepository _dataRepository;

        /// <summary>
        ///     Default Constructor. Creates and instance of the IDataRepository
        /// </summary>
        public InvestmentCategoryService()
        {
            try
            {
                _dataRepository = new DataRepository ();

            }
            catch (NullReferenceException exception)
            {
                // Exception conditions detected when invoking this service as a RESTFul service need to report an HTTP status code in the 
                //  400-500   range in addition to a message that details the exception
                throw new WebProtocolException(HttpStatusCode.BadGateway, "Could not connect to the database.Please check the connection and try again", exception);
            }
            catch (Exception exception)
            {
                throw new WebProtocolException(ExceptionHelper.GetStatusCode(exception), exception.Message, exception);
            }
        }

        /// <summary>
        ///     Overloaded constructor. Injects the given IDataRepository object to be used
        /// </summary>
        /// <param name="repository"> 
        ///     The IDataRepository to be used
        /// </param>
        public InvestmentCategoryService(IDataRepository repository)
        {
            try
            {
                _dataRepository = repository;

            }
            catch (Exception exception)
            {
                throw new WebProtocolException(ExceptionHelper.GetStatusCode(exception), exception.Message, exception);
            }
        }

        /// <summary>
        ///     Method to get a list of all the InvestmentCategorySummaries
        /// </summary>
        /// <returns>
        ///     List of all InvestmentCategorySummaries
        /// </returns>
        public ICollection<InvestmentCategorySummary> GetAll()
        {
            try
            {
                return _dataRepository.GetAllInvestmentCategorySummaries();

            }
            catch (NullReferenceException exception)
            {
                throw new WebProtocolException(HttpStatusCode.InternalServerError , "The Database context was null", exception);
            }
            catch (Exception exception)
            {
                throw new WebProtocolException(ExceptionHelper.GetStatusCode(exception), exception.Message, exception);
            }
        }

       

        #endregion
    }
}
